<?php
/*
 * desc：
 * author：wh
 * email：
 * createTime：{2019/11/13} {19:00} 
 */

namespace libraries\systemlogs;

use libraries\Tools;
use think\Db;
use think\facade\Log;

/**
 * 慢响应
 * Class SysRequestLog
 * @package libraries\systemlogs
 */
class SysRequestLog
{

    static function add($start_time){
        $end_time = Tools::getMillisecond();
        $data = [
            'url'=>request()->baseUrl(),
            'module'=>app('http')->getName(),
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'used_time'=>$end_time - $start_time,
        ];
        try{
            if(config('app.is_slow_response_log') && $data['used_time'] >= config('app.is_slow_response_log')){
                Db::table('log_sys_request')->insert($data);
            }
        }catch (\Exception $e){
            //DATABASE BOOM
            //tp6
            //Log::error('========[数据库异常:DATABASE BOOM]========'.$e->getTraceAsString());
            //Log::close();
            //tp5.1
            Log::write('========[DATABASE ERROR:DATABASE BOOM]========【'.$e->getMessage().'】'.$e->getTraceAsString());
        }
    }
}